//
//  UIButton+Extension.swift
//  WeiBo
//
//  Created by 小小罗 on 15/11/5.
//  Copyright © 2015年 RealMadrid. All rights reserved.
//

import UIKit
//封装一个自定义按钮的方法  这里相当于OC中的分类
extension UIButton {
    
    /// `便利`构造函数
    convenience init(imageName:String, backGroundImageName:String?) {
        
        self.init()
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        if let backGroundImageName = backGroundImageName{
            setBackgroundImage(UIImage(named: backGroundImageName), forState: UIControlState.Normal)
            setBackgroundImage(UIImage(named: backGroundImageName + "_highlighted"), forState: UIControlState.Highlighted)
        }
        
        
        // 会根据背景图片的大小调整尺寸
        sizeToFit()
        
        
    }
    
    
    convenience init(title: String, color: UIColor, backImageName: String) {
        self.init()
        
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        
        setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        
        // 会根据背景图片的大小调整尺寸
        sizeToFit()

    }
    
    /// 便利构造函数
    ///
    /// - parameter title:     title
    /// - parameter color:     color
    /// - parameter fontSize:  字体大小
    /// - parameter imageName: 图像名称
    ///
    /// - returns: UIButton
    convenience init(title: String, fontSize: CGFloat, color: UIColor, imageName: String) {
        self.init()
        
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        setImage(UIImage(named: imageName), forState: .Normal)
        
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        
        sizeToFit()
    }
    
    
    
    
    
}